package tarena.controller.account;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import tarena.comm.MyPage;
import tarena.entity.TabAccount;
import tarena.service.TabAccountService;

import javax.validation.Valid;

/**
 * 管理员管理用户的Controller.
 * 
 * @author calvin
 */
@Controller
@RequestMapping(value = "/admin/user")
public class UserAdminController {

	@Autowired
	private TabAccountService tabAccountService;

//	@RequestMapping(method = RequestMethod.GET)
//	public String list(Model model) {
//		List<TabAccount> tabAccounts = tabAccountService.getAll();
//		model.addAttribute("tabAccounts", tabAccounts);
//
//		return "account/adminUserList";
//	}
	@RequestMapping(value = "list/page/{pagenum}", method = RequestMethod.GET)
	public String list(@PathVariable String pagenum,Model model) {
		MyPage<TabAccount> TabAccount = tabAccountService.searchPage(Integer.parseInt(pagenum), 5);
		model.addAttribute("myPage", TabAccount);
		return "account/adminUserList";
	}
	@RequestMapping(value = "listType/page/{pagenum}" , method = RequestMethod.GET)
	public String tepylist(@Valid @ModelAttribute("key")String key,@PathVariable String pagenum,Model model) {
		MyPage<TabAccount> TabAccount = tabAccountService.searchPageByName(key,Integer.parseInt(pagenum), 5);
		model.addAttribute("myPage", TabAccount);
		return "account/adminUserListType";
	}

	@RequestMapping(value = "update/{id}/{currentPage}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id,@PathVariable(value = "currentPage") Long currentPage, Model model) {
		model.addAttribute("currentPage", currentPage);
		model.addAttribute("tabAccount", tabAccountService.getById(id));
		return "account/adminUserForm";
	}
	@RequestMapping(value = "searchUpdate/{id}/{currentPage}", method = RequestMethod.GET)
	public String SearchupdateForm(@PathVariable("id") Long id,@PathVariable(value = "currentPage") Long currentPage,@RequestParam("key") String key, Model model) {
		model.addAttribute("currentPage", currentPage);
		model.addAttribute("key", key);
		model.addAttribute("tabAccount", tabAccountService.getById(id));
		return "account/adminUserForm";
	}
	@RequestMapping(value = "lookForm/{id}", method = RequestMethod.GET)
	public String lookForm(@PathVariable("id") Long id, Model model) {
		model.addAttribute("user", tabAccountService.getById(id));
		return "account/userLook";
	}

	@RequestMapping(value = "update/{currentPage}", method = RequestMethod.POST)
	public ModelAndView update(@Valid @ModelAttribute("tabAccount") TabAccount tabAccount, @PathVariable(value = "currentPage") Long currentPage,@RequestParam("key") String key, RedirectAttributes redirectAttributes) {
		tabAccountService.update(tabAccount);
		redirectAttributes.addFlashAttribute("message", "更新用户" + tabAccount.getLoginName() + "成功");
		return new ModelAndView("redirect:/admin/user/listType/page/"+currentPage).addObject("key",key);
	}

	@RequestMapping(value = "delete/{id}/{currentPage}")
	public String delete(@PathVariable("id") Long id,@PathVariable(value = "currentPage") Long currentPage, RedirectAttributes redirectAttributes) {
		TabAccount tabAccount = tabAccountService.getById(id);
		tabAccountService.delete(id);
		redirectAttributes.addFlashAttribute("message", "删除用户" + tabAccount.getLoginName() + "成功");
		return "redirect:/admin/user/list/page/"+currentPage;
	}
	@RequestMapping(value = "searchDelete/{id}/{currentPage}")
	public ModelAndView Searchdelete(@PathVariable("id") Long id,@PathVariable(value = "currentPage") Long currentPage,@RequestParam("key") String key, RedirectAttributes redirectAttributes) {
		TabAccount tabAccount = tabAccountService.getById(id);
		tabAccountService.delete(id);
		redirectAttributes.addFlashAttribute("message", "删除用户" + tabAccount.getLoginName() + "成功");
		return new ModelAndView("redirect:/admin/user/listType/page/"+currentPage).addObject("key",key);
	}
	@RequestMapping(value = "userGraph",method = RequestMethod.GET)
	public String userGraph(Model model) {
		model.addAttribute("tabAccount", tabAccountService.getAll());
		return "statistics/userGraph";
	}

	/**
	 * 所有RequestMapping方法调用前的Model准备方法, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出User对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此仅在update时实际执行.
	 */
	@ModelAttribute
	public void getUser(@RequestParam(value = "id", defaultValue = "-1") Long id, Model model) {
		if (id != -1) {
			model.addAttribute("tabAccount", tabAccountService.getById(id));
		}
	}
}
